<template>
  <el-container>
    <el-header>
      <el-row type="flex" justify="space-between">
        <span>后台管理系统</span>
        <el-button type="info" @click="logout">退出</el-button>
      </el-row>
    </el-header>
    <el-container>
      <!-- 折叠时候宽度只有64px -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse()">|||</div>
        <!-- 侧边栏 unique-opened 是否只保持一个子菜单的展开
            router 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
            collapse-transition	是否开启折叠动画
            default-active	当前激活菜单的 index
        -->
        <el-menu
          unique-opened
          background-color="#b4cdcd"
          text-color="#262626"
          active-text-color="#CD5555"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="currActiveMenu"
        >
          <el-submenu :index="item.id+''" v-for="(item) in menuList" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 名称 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveActiveMenu('/'+subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      currActiveMenu: '' //当前激活的菜单
    }
  },
  methods: {
    //退出
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //获取菜单
    async getMenus() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        this.menuList = res.data
      }
    },
    //点击按钮菜单的展开或者折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    //保存当前点击的菜单
    saveActiveMenu(activeMenu) {
      this.currActiveMenu = activeMenu
      //保存起来，刷新的时候也能用
      window.sessionStorage.setItem('activeMenu', activeMenu)
    }
  },
  created() {
    this.getMenus()
    //刷新时候获取当前的菜单
    this.currActiveMenu = window.sessionStorage.getItem('activeMenu')
  }
}
</script>

<style lang="less" scoped>
/* element中，元素名可以当做类名直接使用 */
.el-header {
  background-color: #aeeeee;
  .el-row {
    height: 100%;
    align-items: center;
    font-size: 20px;
    color: #000;
  }
}
.el-aside {
  background-color: #b4cdcd;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #f5fffa;
}
.el-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>